﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace CPPEI.Net.TCP
{
    /// <summary>
    /// 心跳线程
    /// </summary>
    public class TCPHeartbeatThread
    {
        private Thread _thread;
        private bool _isRunning;
        private EventWaitHandle _waitHandle;

        private TCPClient _client;

        public TCPHeartbeatThread(TCPClient client)
        {
            _client = client;
        }

        public void Start()
        {
            _waitHandle = new EventWaitHandle(false, EventResetMode.ManualReset);
            _thread = new Thread(Running);
            _isRunning = true;
            _thread.Start();
        }

        public void Stop()
        {
            _isRunning = false;
            _waitHandle.WaitOne();
        }

        private void Running()
        {
            while (_isRunning)
            {
                long curTicks = DateTime.Now.Ticks;
                if (curTicks - _client.LatestActiveTicks > TimeSpan.FromSeconds(2).Ticks)
                {
                    // 发动心跳信号到服务器
                }
            }
            _waitHandle.Set();
        }
    }
}
